package arithmetic.changba;

import java.util.Scanner;

/**
 * 旋转打印二维数组
 *
 * 已知有数组
 *
 * 1	2	3	4 5	6	7	8 9	10	11	12 13	14	15	16 要求按照  1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10的顺序打印出来
 */
public class MatrixVisit {

    public static void main(String[] args) {
        new MatrixVisit().init();
    }

    public void init() {
        int[][] a = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};

        rotatePrint(a);

    }

    public void rotatePrint(int arr[][]) {
        int xStart = 0, xEnd = arr[0].length - 1;
        int yStart = 0, yEnd = arr.length - 1;

        // 一圈圈的打印,顺时针旋转
        while (xEnd >= xStart && yEnd >= yStart) {
            for (int i = xStart; i <= xEnd; i++) {
                // 注意数组里后一个表示x，前一个表示y
                System.out.println(arr[yStart][i]);
            }
            yStart++;
            for (int j = yStart; j <= yEnd; j++) {
                System.out.println(arr[j][xEnd]);
            }
            xEnd--;
            for (int i = xEnd; i >= xStart; i--) {
                System.out.println(arr[yEnd][i]);
            }
            yEnd--;
            for (int j = yEnd; j >= yStart; j--) {
                System.out.println(arr[j][xStart]);
            }
            xStart++;
        }

    }




}
